// 鼠标按钮事件处理示例。
//
// 演示如何处理鼠标左键、中键、右键单击。

package main

import (
	"fmt"
	"log"

	"github.com/gotk3/gotk3/gdk"
	"github.com/gotk3/gotk3/gtk"
)

func main() {
	gtk.Init(nil)

	win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
	if err != nil {
		log.Fatal("无法创建窗口：", err)
	}
	win.SetTitle("鼠标事件")
	win.Connect("destroy", func() {
		gtk.MainQuit()
	})

	win.Add(windowWidget())
	win.ShowAll()

	gtk.Main()
}

func windowWidget() *gtk.Widget {
	lbl, err := gtk.LabelNew("使用鼠标按钮")
	lbl.SetSizeRequest(500, 200)
	if err != nil {
		panic(err)
	}
	evtBox, err := gtk.EventBoxNew()
	if err != nil {
		panic(err)
	}
	evtBox.Add(lbl)
	evtBox.Connect("button-press-event", func(tree *gtk.EventBox, ev *gdk.Event) bool {
		btn := gdk.EventButtonNewFromEvent(ev)
		fmt.Println("按下按钮")
		switch btn.Button() {
		case gdk.BUTTON_PRIMARY:
			lbl.SetText("检测到左键单击！")
			return true
		case gdk.BUTTON_MIDDLE:
			lbl.SetText("检测到中键！")
			return true
		case gdk.BUTTON_SECONDARY:
			lbl.SetText("检测到右键！")
			return true
		default:
			return false
		}
	})

	return &evtBox.Widget
}
